var serverUrl = "https://smart-dev.gtdreamlife.com:18762";
var accessToken = null;

var deviceList = null;
var eventList = null;
var projectList = null;
var customerList = null;
var memberList = null;
var vehicleList = null;
var townList = null;
var deviceTypeList = null;
$(document).ready(function() {
	$("#password").val("123456");
});

$("#codeImg").click(function() {
	reloadCode();
});

function reloadCode() {
	var vCodeId = parseInt((Math.random() * 100000000 + ""));
	var codeImageUrl = serverUrl + "/kaptcha/vcode?vCodeId=" + vCodeId;
	$("#vCodeId").val(vCodeId);
	$("#codeImg").attr("src", codeImageUrl);
}

reloadCode();


$("#selectServer").change(function() {
	var serverType = $("#selectServer").val();

});


function initSelect(uiSelect, list, key) {
	$(uiSelect).html('');
	$(uiSelect).prepend("<option value=''>请选择</option>"); //添加第一个option值
	for (var i = 0; i < list.length; i++) {
		$(uiSelect).append("<option value=" + i + ">" + list[i][key] + "</option>");
	}
}

function reqSync(url, method) {
	var result = null;
	if (accessToken == null) {
		alert("请先登录");
		return result;
	}
	var author = 'Bearer ' + accessToken;
	$.ajax({
		url: url,
		type: method,
		async: false, //使用同步的方式,true为异步方式
		headers: {
			'Content-Type': 'application/json;charset=utf8;',
			'Authorization': author
		},
		success: function(data) {
			console.log(data);
			result = data;
		},
		error: function(data) {
			console.log("error");
		}
	});
	return result;
}

function getSelectData(selectUi, list) {
	var index = $(selectUi).val();
	if (index == "") {
		return {};
	}
	return list[parseInt(index)];
}




function getProject() {
	var url = serverUrl + '/api/ioc/user/projects';
	var result = reqSync(url, "post");
	projectList = result.data;
	initSelect("#selectProject", projectList, "name");
}


function getTown() {
	var url = serverUrl + '/api/ioc/user/towns';
	var result = reqSync(url, "post");
	townList = result.data;
	initSelect("#selectTown", townList, "name");
}

function getDevice() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var iocDeviceType = getSelectData("#selectEvent", eventList).iocDeviceType;
	if (projectId == null) {
		alert("请先选择项目");
		return;
	}
	// if (iocDeviceType == null) {
	// 	alert("请先选择事件类型");
	// 	return;
	// }

	iocDeviceType = getSelectData("#selectDeviceType", deviceTypeList).type;

if(iocDeviceType==null){
	iocDeviceType ="";
}
	var url = serverUrl + '/api/device/manage/list?projectId=' + projectId + '&iocDeviceType=' + iocDeviceType;
	var result = reqSync(url, "get");
	deviceList = result.data;
	initSelect("#selectDevice", deviceList, "deviceName");
}

function getDeviceType() {
	var url = serverUrl + '/api/device/manage/iocDeviceTypes?projectId=t1';
	var result = reqSync(url, "get");
	deviceTypeList = result.data;
	initSelect("#selectDeviceType", deviceTypeList, "name");
}


function getEventType() {
	var url = serverUrl + '/api/event/rest/eventTypes';
	var result = reqSync(url, "post");
	eventList = result.data;
	initSelect("#selectEvent", eventList, "abnormalInfo");
}

function getCustomer() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var identity = $("#selectPersonType").val();

	var townId = "";
	var eventType = getSelectData("#selectEvent", eventList).eventType;
	if (eventType != null && projectId != null) {
		if (eventType == "BlacklistAlarm") {
			townId = getSelectData("#selectTown", townList).id;
		}
	}
	var url = serverUrl + '/api/aep/mock/customers?projectId=' + projectId + "&identity=" + identity + "&townId=" + townId;
	var result = reqSync(url, "get");
	customerList = result.data;
	initSelect("#selectCustomer", customerList, "name");
}

function getVechile() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var identity = $("#selectPersonType").val();
	identity = $("#selectCarType").val();
	var url = serverUrl + '/api/aep/mock/vehicles?projectId=' + projectId + "&identity=" + identity;
	var result = reqSync(url, "get");
	vehicleList = result.data;
	initSelect("#selectVehicle", vehicleList, "plateNumber");
}

function getMember() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var customerId = getSelectData("#selectCustomer", customerList).id;
	var url = serverUrl + '/api/aep/mock/members?projectId=' + projectId + "&customerId=" + customerId;
	var result = reqSync(url, "get");
	memberList = result.data;
	initSelect("#selectMember", memberList, "name");
}


function showPic() {
	if ($("#picUrl").val() != "") {
		$("#imgShow").attr("src", $("#picUrl").val());
	}
}

function showsmallPic() {
	if ($("#smallPicUrl").val() != "") {
		$("#smallImg").attr("src", $("#smallPicUrl").val());
	}
}


$("#getProjects").click(function() {
	getProject();
});


$("#getDevices").click(function() {
	getDevice();
});


$("#getEventTypes").click(function() {
	getEventType();
});



$("#getCustomers").click(function() {
	getCustomer();
});
$("#selectDeviceType").change(function() {
	getDevice();
});
$("#exportExcel").click(function() {
	var author = 'Bearer ' + accessToken;
	var url = "/api/ioc/report/passenger/flow/day/export";
	$.ajax({
		url: url,
		type: "post",
		async: false, //使用同步的方式,true为异步方式
		data: $("#contidion").val(),
		headers: {
			'Content-Type': 'application/json;charset=utf8;',
			'Authorization': author
		},
		success: function(data) {
			console.log(data);
			result = data;
		},
		error: function(data) {
			console.log("error");
		}
	});
});



$("#selectEvent").change(function() {
	var eventType = getSelectData("#selectEvent", eventList).eventType;
	var projectId = getSelectData("#selectProject", projectList).id;
	if (eventType != null && projectId != null) {
		getDevice();
		$("#trafficD").hide();
		if (eventType == "PersonTraffic") {
			$("#trafficD").show();
			$("#customerD").show();
			$("#vehicleD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
		} else if (eventType == "carTraffic") {
			$("#vehicleD").show();
			$("#trafficD").show();
			$("#customerD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
		} else if (eventType == "MobileSafetyGuard") {
			$("#deviceT").hide();
			$("#customerD").show();
			$("#vehicleD").hide();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
		} else if (eventType == "BlacklistAlarm") {
			$("#customerD").show();
			$("#vehicleD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
			//$("#similarityD").show();
		} else if (eventType == "HouseArmAlarm") {
			$("#customerD").show();
			$("#vehicleD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
		} else if (eventType == "MemberLeaveThePark") {
			$("#customerD").show();
			$("#vehicleD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").show();
			//$("#similarityD").show();
		} else {
			$("#customerD").hide();
			$("#vehicleD").hide();
			$("#deviceT").show();
			$("#tempVehicleD").hide();
			$("#memberD").hide();
			//$("#similarityD").hide();
		}
	}
});


$("#selectProject").change(function() {
	var eventType = getSelectData("#selectEvent", eventList).eventType;
	var projectId = getSelectData("#selectProject", projectList).id;
	if (eventType != null && projectId != null) {
		getDevice();
	}
});
$("#selectCustomer").change(function() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var customerId = getSelectData("#selectCustomer", customerList).id;
	if (projectId != null && customerId != null) {
		getMember();
	}

	var identity = $("#selectPersonType").val();
	if (identity == "3") {
		var facePic = getSelectData("#selectCustomer", customerList).facePic;
		if (facePic != "") {
			$("#blackPic").attr("src", facePic);
		}
	}
});






$("#selectPersonType").change(function() {
	var identity = $("#selectPersonType").val();
	if (identity == "0") {
		$("#customerTitle").html("访客:");
		$("#tempVehicleD").hide();
		$("#vehicleD").show();
	} else if (identity == "1") {
		$("#customerTitle").html("业主:");
		$("#tempVehicleD").hide();
		$("#vehicleD").show();
	} else if (identity == "3") {
		$("#customerD").show();
		$("#customerTitle").html("黑名单:");
		$("#tempVehicleD").hide();
		$("#vehicleD").hide();
		$("#tempVehicleD").show();
	} else if (identity == "2") {
		var eventType = getSelectData("#selectEvent", eventList).eventType;
		if (eventType == "carTraffic") {
			$("#vehicleD").hide();
			$("#tempVehicleD").show();
		}
	}

	if (identity == "1" || identity == "0") {
		getCustomer();
		getVechile();
	}
	if (identity == "3") {
		getCustomer();
	}
});

$("#selectCarType").change(function() {
	var identity = $("#selectCarType").val();
	var eventType = getSelectData("#selectEvent", eventList).eventType;
	if (eventType == "carTraffic") {
		getVechile();
	}
});


$("#selectMember").change(function() {
	var faceUrl = getSelectData("#selectMember", memberList).faceUrl;

	if (faceUrl != "") {
		$("#imgMember").attr("src", faceUrl);
	}

});






$("#pushEvent").click(function() {
	var projectId = getSelectData("#selectProject", projectList).id;
	var townId = getSelectData("#selectTown", townList).id;
	var eventType = getSelectData("#selectEvent", eventList).eventType;
	var uniqueId = getSelectData("#selectDevice", deviceList).uniqueId;
	var plateNumber = getSelectData("#selectVehicle", vehicleList).plateNumber;
	var identity = $("#selectPersonType").val();




	var innOrOut = $("#selectInOrOut").val();
	var memberId = getSelectData("#selectMember", memberList).personId;
	var memberName = getSelectData("#selectMember", memberList).name;
	var personName = getSelectData("#selectCustomer", customerList).name;
	var customerId = getSelectData("#selectCustomer", customerList).id;



	var similarity = $("#similarity").val();
	var eventPicUrl = $("#picUrl").val();
	var parkingSpace = $("#selectParkingSpace").val();

	var trafficType = $("#selectTrafficType").val();
	var sex = $("#selectSex").val();
	var ageGroup = $("#selectAgeGroup").val();
	var alarmValue = $("#alarmValue").val();


	if (eventType == 'carTraffic') {
		identity = $("#selectCarType").val();
	}
	if (eventType == 'personTraffic') {

		var identity = $("#selectPersonType").val();


	}
	var personPhone = "";
	if (identity == "1") {
		personPhone = getSelectData("#selectCustomer", customerList).mobile;
	}


	/* if (eventType == "carTraffic" || eventType == "PersonTraffic") {
		identity = null;
	} */

	console.log(projectId + "-" + eventType + "-" + uniqueId + "-" + customerId);
	var url = serverUrl + '/api/aep/mock/common';

	var snapFacePicUrl = $("#smallPicUrl").val();

	if (plateNumber == null) {
		plateNumber = $("#tempVehicle").val();
	}

	var json = {
		"projectId": projectId,
		"eventType": eventType,
		"uniqueId": uniqueId,
		"memberId": memberId,
		"memberName": memberName,
		"customerId": customerId,
		"eventPicUrl": eventPicUrl,
		"identity": identity,
		"personName": personName,
		"plateNumber": plateNumber,
		"innOrOut": innOrOut,
		"snapFacePicUrl": snapFacePicUrl,
		"townId": townId,
		"similarity": similarity,
		"trafficType": trafficType,
		"sex": sex,
		"ageGroup": ageGroup,
		"parkingSpace": parkingSpace,
		"carOwnerPhone": personPhone,
		"alarmValue":alarmValue
	};
	console.log(JSON.stringify(json));
	if (accessToken == null) {
		alert("请先登录");
		return result;
	}
	var author = 'Bearer ' + accessToken;
	$.ajax({
		url: url,
		type: "post",
		data: JSON.stringify(json),
		headers: {
			'Content-Type': 'application/json;charset=utf8;',
			'Authorization': author
		},
		success: function(data) {
			console.log(data.data);
		},
		error: function(data) {
			console.log("error");
		}
	});
});

$("#loginOut").click(function() {
	accessToken = null;
	$("#loginOut").hide();
	$("#loginBtn").show();
});

$("#loginBtn").click(function() {
	var username = $("#username").val();
	var password = $("#password").val();
	var random_num = Math.random();
	var hash = hex_md5(password);

	var code = $("#vCode").val();
	var vCodeId = $("#vCodeId").val();
	var params = {
		scope: "server",
		grant_type: "password",
		username: username,
		password: hash,
		random_num: random_num,
		vCodeId: vCodeId,
		code: code
	};
	$.ajax({
		url: serverUrl + '/api/auth/oauth/token',
		type: 'post',
		dateType: 'json',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			'Authorization': 'Basic aW9jOmlvYw=='
		},
		data: params,
		success: function(data) {
			console.log(data);
			console.log(data.access_token);
			accessToken = data.access_token;
			console.log("登录成功");
			$("#loginOut").show();
			$("#loginBtn").hide();

			// 初始化
			getEventType();
			getProject();
			getTown();
			getDeviceType();
			$("#loginMessage").html("登录成功");
		},
		error: function(data) {
			reloadCode();
			$("#loginMessage").html(data.responseJSON.errorMessage);
			console.log("error");
		}
	});
});
